package arg

import (
	"encoding/json"
	"fmt"
	"github.com/gurkankaymak/hocon"
	"db2s/global"
	"reflect"
	"strings"
)

type loadLevel struct {
	Mode         string `json:"mode"`
	FilePath     string `json:"filePath"`
	Safe         bool   `json:"safe"`
	Gzip         bool   `json:"gzip"`
	FixRows      int    `json:"fixRows"`
	SqlMode      bool   `json:"sqlmode"`
	IgColumn     bool   `json:"igColumn"`
	ColumnSplist string `json:"column"`
	RowsSplist   string `json:"rows"`
	Truncate     bool   `json:"truncate"`
}

// loadRuleSecondaryLevelParameterCheck
func loadRuleParameter(hocon *hocon.Config) (Load LoadS, err error) {
	var (
		loadMap map[string]any
		//err     error
		//f3      = rc.FirstL1.Rules.ToConfig().GetObject(rc.getObjectName(checkMod)).ToConfig()
	)
	//rc.SecondaryL.RulesV.CheckMode = rc.getObjectName(checkMod)
	//rc.SecondaryL.FunctionalMode = rc.getObjectName(checkMod)
	if loadMap, err = global.StructToMapAny(getLoadRules()); err != nil {
		return
	}
	var s1 []string
	for k, v := range loadMap {
		if r1 := rc.getHoconConfigVal(hocon, k, reflect.TypeOf(v).String()); r1 != nil {
			s1 = append(s1, rc.anyToJsonString(k, r1))
		} else {
			s1 = append(s1, rc.anyToJsonString(k, v))
		}
	}
	var s2 string
	if strings.EqualFold(s2, "{}") {
		s2 = fmt.Sprintf("\"%s\":{%s}", "load", strings.Join(s1, ","))
	} else {
		s2 = fmt.Sprintf("{%s}", strings.Join(s1, ","))
	}
	if err = json.Unmarshal([]byte(strings.ReplaceAll(s2, "\\", "\\\\")), &Load); err != nil {
		return
	}
	Load.ActiveSwitch = ActiveSwitch{TableAccessPriInit: false, TableStructInit: true, GlobalParameterInit: true, TableIndexInit: true, TableIndexFilter: true,
		TableTruncate: Load.Truncate, TableRecover: false, PartitionInit: true}
	return
}

func loadParameterCheck(s *SecondaryLevel) error {
	if strings.EqualFold(s.RulesV.Load.LoadMode, "output") {
		s.RulesV.Load.Truncate = false
	}
	return nil
}
